<template>
  <div id="bg03">
    <div id="el-login03">
      <h1>欢迎来到玺玛特</h1>
      <img src="/imgs/logo/logo.png" style="width: 60px" alt="">
      <el-tabs type="border-card"
               style="width: 400px;
                      height: 500px;
                      margin: 0 auto;
                      background-color:rgba(255,255,255,0.3)">
        <el-tab-pane label="快速注册账号">
          <!--            label-width设置每个表单项 文本标签的宽度-->
          <el-form  :model="ruleForm" :rules="rules" ref="ruleForm"  class="demo-ruleForm"
                    style="width: 300px;margin: 20px auto" label-width="80px">
            <el-form-item label="用户名" style="color: black;">
              <el-input type="text" placeholder="请输入用户名" v-model="ruleForm.username"></el-input>
            </el-form-item>
            <el-form-item label="用户密码" style="color: black">
              <el-input type="text" placeholder="请输入用户密码" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" style="color: black">
              <el-input type="text" placeholder="请再次输入用户密码" v-model="ruleForm.repassword"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" style="color: black">
              <el-input type="text" placeholder="请输入联系方式" v-model="ruleForm.phone"></el-input>
            </el-form-item>
            <el-form-item label="用户昵称" style="color: black">
              <el-input type="text" placeholder="请输入用户昵称" v-model="ruleForm.nickname"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button style="position: relative;right: 30px" type="primary">登录</el-button>
            </el-form-item>
          </el-form>
          <el-a style="text-decoration: none;color: #3a8ee6;text-align: center"><a href="../admin/login">已有账号，去登录</a></el-a>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password:"",
        repassword:"",
        phone:"",
        nickname:"",
        delivery: false,
      },
      rules: {
        username: [
          { required: true, trigger: 'blur' },
          { min: 4, max: 16, message: '4到16的字母数字组合', trigger: 'blur' }
        ],
        password:[
          { required: true,  trigger: 'blur' },
          { min: 6, max: 20, message: '6-20位字母和数字组合', trigger: 'blur' }
        ],
        repassword:[
          { required: true,  trigger: 'blur' },
          { message: '请再次输入密码', trigger: 'blur' }
        ],
        phone:[
          { required: true,  trigger: 'blur' },
          { pattern:/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '请填写正确的手机号', trigger: 'blur' }
        ],

      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          let url = 'http://localhost:9081/users/login';
          console.log('url >>> ' + url);
          let data = {
            'username': this.ruleForm.username,
            'password': this.ruleForm.password,
            'phone':this.ruleForm.phone,
            'nickname':this.ruleForm.nickname,
          };
          console.log('data >>> ');
          console.log(data);
          this.axios.post(url, data).then((response) => {
            console.log(response.data.code);
            if (response.data.code == 20000) {
              // 提示登录成功
              this.$message({
                message: '注册成功！正在跳转登录页面...',
                type: 'success'
              });
              // 获取服务器端响应的JWT并存入到LocalStorage中
              let jwt = response.data.data;
              console.log(jwt);
              localStorage.setItem('jwt', jwt);
              // 跳转到主页
              this.$router.push('/user/login');
            } else {
              this.$message.error('注册失败，请稍后再试！');
            }
          }).catch(function () {
            console.log('响应结果为失败！');
          })
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
#bg03 {
  background-repeat: no-repeat;
  background-image: url("/src/views/user/bg02.png");
  background-size: 70%;
  background-position: center;
  background-color:black;
  margin: 0;
  overflow: hidden;
}

#el-login03 {
  color: #3a8ee6;
  width: 500px;
  height: 675px;
  margin-left: 1050px;
  margin-top: 20px;

}

</style>
